USE [RD_ARMart]
GO
/****** Object:  StoredProcedure [dbo].[USP_AR_Star_Adjustments_Refresh]    Script Date: 5/30/13 1:01:31 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[USP_AR_Star_Adjustments_Refresh]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE        PROCEDURE [dbo].[USP_AR_Star_Adjustments_Refresh]
AS
----------------------------------------------------------------------------------------------------
--	Author:			c Copyright 2004 Andrews Consulting Group 
--	Procedure:		USP_AR_Star_Adjustments_Refresh
--
--	Description:		User Stored Procedure To Update RD_ARMart.dbo.AR_Star_Facts
--				Deduction columns.  This User	
--				Stored Procedure Must Be Executed After Refresh Of 
-- 				AR_Facts - JDEOWARAdjustments. 
--
--	Application:		Accounts Receivable Data Mart
--
--
--Modification History
--
--Date			Modified By		Description
--01.01.2001 		<developer name>	<comments>
-----------------------------------------------------------------------------------------------------------

------------------------------------------------------------------------------------------------------------
---- Remarked out MT 08/05/2003  -- DATEID is mapped to invoice date in subscription
------------------------------------------------------------------------------------------------------------
--UPDATE
--	RD_ARMart.dbo.AR_Star_Facts
--SET
--	DateID = Cast(Substring(convert(char(10),GLDateID,101),7,4) + 
--		Substring(convert(char(10),GLDateID,101),1,2) + 
--		Substring(convert(char(10),GLDateID,101),4,2) as Int)   
--FROM 
--	RD_ARMart.dbo.AR_Star_Facts Facts
--WHERE
--	InvoiceType=''C''
------------------------------------------------------------------------------------------------------------


--DeductionID
UPDATE
	RD_ARMart.dbo.AR_Star_Facts
SET
	DeductionID = DM.DeductionID
FROM
	RD_ARMart.dbo.AR_Star_Facts Facts
	INNER JOIN RD_ARWork.dbo.JDEOWARARDeductionManagement DM ON
		Facts.SourceID		= DM.SourceID AND
		Facts.DocumentNumber 	= DM.ClaimDocumentNumber AND
		Facts.DocumentType	= DM.ClaimDocumentType
WHERE
	InvoiceType=''A''

-- ''C'' DeductionActivityCode
UPDATE 
	RD_ARMart.dbo.AR_Star_Facts
SET
	DeductionActivityCode 	= ISNULL(DA.ActivityReasonCode,''UA'')
FROM
	RD_ARMart.dbo.AR_Star_Facts Facts
	LEFT JOIN RD_ARWork.dbo.JDEOWARARDeductionActivity DA ON
		Facts.SourceID		= DA.SourceID AND
		Facts.DeductionID	= DA.DeductionID
WHERE
	InvoiceType=''A''
' 
END
GO
ALTER AUTHORIZATION ON [dbo].[USP_AR_Star_Adjustments_Refresh] TO  SCHEMA OWNER 
GO
/****** Object:  StoredProcedure [dbo].[USP_AR_Star_CheckDetail_Refresh]    Script Date: 5/30/13 1:01:31 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[USP_AR_Star_CheckDetail_Refresh]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE        PROCEDURE [dbo].[USP_AR_Star_CheckDetail_Refresh]
AS
----------------------------------------------------------------------------------------------------
--	Author:			c Copyright 2004 Andrews Consulting Group 
--	Procedure:		USP_AR_Star_CheckDetail_Refresh
--
--	Description:		User Stored Procedure To Update RD_ARMart.dbo.AR_Star_Facts
--				Deduction columns.  This User	
--				Stored Procedure Must Be Executed After Refresh Of 
-- 				AR_Facts - JDEOWARCheckDetail. 
--
--	Application:		Accounts Receivable Data Mart
--
--
--Modification History
--
--Date			Modified By		Description
--01.01.2001 		<developer name>	<comments>
-----------------------------------------------------------------------------------------------------------


--DeductionID
UPDATE
	RD_ARMart.dbo.AR_Star_Facts
SET
	DeductionID = DM.DeductionID
FROM
	RD_ARMart.dbo.AR_Star_Facts Facts
	INNER JOIN RD_ARWork.dbo.JDEOWARARDeductionManagement DM ON
		Facts.SourceID		= DM.SourceID AND
		Facts.DocumentNumber 	= DM.ClaimDocumentNumber AND
		Facts.DocumentType	= DM.ClaimDocumentType
WHERE
	InvoiceType=''C''

-- ''C'' DeductionActivityCode
UPDATE 
	RD_ARMart.dbo.AR_Star_Facts
SET
	DeductionActivityCode 	=	ISNULL(DA.ActivityReasonCode,''UA'')
FROM
	RD_ARMart.dbo.AR_Star_Facts Facts
	LEFT JOIN RD_ARWork.dbo.JDEOWARARDeductionActivity DA ON
		Facts.SourceID		=	DA.SourceID AND
		Facts.DeductionID	=	DA.DeductionID
WHERE
	InvoiceType=''C''


UPDATE 
	RD_ARMart.dbo.AR_Star_Facts
SET
	CashReceived 	=	ISNULL(CR.AmountCheckAmount,0)
FROM
	RD_ARMart.dbo.AR_Star_Facts Facts
	LEFT JOIN RD_ARWork.dbo.JDEOWARReceiptsHeaderFile CR ON
		Facts.SourceID		=	CR.SourceID AND
		Facts.InternalPaymentID	=	CR.InternalPaymentID
WHERE
	InvoiceType=''C''
' 
END
GO
ALTER AUTHORIZATION ON [dbo].[USP_AR_Star_CheckDetail_Refresh] TO  SCHEMA OWNER 
GO
/****** Object:  StoredProcedure [dbo].[USP_AR_Star_Customer_Refresh]    Script Date: 5/30/13 1:01:31 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[USP_AR_Star_Customer_Refresh]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE               PROCEDURE [dbo].[USP_AR_Star_Customer_Refresh]
AS
-------------------------------------------------------------------------------
--	Author:			c Copyright 2004 Andrews Consulting Group
--	Procedure:		USP_AR_Star_Customer_Refresh
--
--	Description:		Create the AR Star Customer Dimension
--					
--	Application:		Accounts Receivable Mart
--
--Modification History
--
--Date			Modified By		Description
--07.21.2003 					SP created
-------------------------------------------------------------------------------

-- Write out to log 
EXEC master.dbo.xp_logevent 65000,''Beginning of Customer After refresh Procedure'',Informational

----------------------------------------------------------------------------------------------------------------------------
--   This section will update the Shipto information and then use the Related addresss  
--  Field.   This will use the Related addressNumber 1 -6 and also the F0150 for the parent 
----------------------------------------------------------------------------------------------------------------------------


----  UPDATE the AR Customer info from the JDEGSAddressBookMaster
UPDATE RD_ARMart.dbo.AR_Star_Customer
SET 
	BillToNumber = Case 
		When SSC.RelatedAddressNumber = 2 Then ABM.RelatedAddressNumber2
		When SSC.RelatedAddressNumber = 3 Then ABM.RelatedAddressNumber3
		When SSC.RelatedAddressNumber = 4 Then ABM.RelatedAddressNumber4
		When SSC.RelatedAddressNumber = 5 Then ABM.FactorSpecialPayee
		When SSC.RelatedAddressNumber = 6 Then ABM.RelatedAddressNumber5
		When SSC.RelatedAddressNumber = 7 Then ABM.RelatedAddressNumber1
		When SSC.RelatedAddressNumber = 1 Then 
				ISNULL((Select  ParentNumber from RD_CommonWork.dbo.JDEABAddressBookORGStructure  ABOS 
					where SSC.CustomerNumber = ABOS.AddressNumber AND
						SSC.SourceID = ABOS.SourceID AND 
						ABOS.StructureType = '' ''), SSC.CustomerNumber)
		Else SSC.CustomerNumber
		End,
	ParentNumber	= Case 
		When SSC.RelatedAddressNumber = 2 Then ABM.RelatedAddressNumber2
		When SSC.RelatedAddressNumber = 3 Then ABM.RelatedAddressNumber3
		When SSC.RelatedAddressNumber = 4 Then ABM.RelatedAddressNumber4
		When SSC.RelatedAddressNumber = 5 Then ABM.FactorSpecialPayee
		When SSC.RelatedAddressNumber = 6 Then ABM.RelatedAddressNumber5
		When SSC.RelatedAddressNumber = 7 Then ABM.RelatedAddressNumber1
		When SSC.RelatedAddressNumber = 1 Then 
				ISNULL((Select  ParentNumber from RD_CommonWork.dbo.JDEABAddressBookORGStructure  ABOS 
					where SSC.CustomerNumber = ABOS.AddressNumber AND
						SSC.SourceID = ABOS.SourceID AND 
						ABOS.StructureType = '' ''), SSC.CustomerNumber)
		Else SSC.CustomerNumber
		End,
	CustomerName	= ABM.AlphaName,
	Category01Code	= ABM.Category01Code,
	Category01	= ABM.Category01,
	Category02Code	= ABM.Category02Code,
	Category02	= ABM.Category02,
	Category03Code	= ABM.Category03Code,
	Category03	= ABM.Category03,
	Category04Code	= ABM.Category04Code,
	Category04	= ABM.Category04,
	Category05Code	= ABM.Category05Code,
	Category05	= ABM.Category05,
	Category06Code	= ABM.Category06Code,
	Category06	= ABM.Category06,	
	Category07Code	= ABM.Category07Code,
	Category07	= ABM.Category07,
	Category08Code	= ABM.Category08Code,
	Category08	= ABM.Category08,
	Category09Code	= ABM.Category09Code,
	Category09	= ABM.Category09,	
	Category10Code	= ABM.Category10Code,
	Category10	= ABM.Category10,
	Category11Code	= ABM.Category11Code,
	Category11	= ABM.Category11,	
	Category12Code	= ABM.Category12Code,
	Category12	= ABM.Category12,
	Category13Code	= ABM.Category13Code,
	Category13	= ABM.Category13,
	Category14Code	= ABM.Category14Code,	
	Category14	= ABM.Category14,
	Category15Code	= ABM.Category15Code,
	Category15	= ABM.Category15,
	Category16Code	= ABM.Category16Code,
	Category16	= ABM.Category16,
	Category17Code	= ABM.Category17Code,
	Category17	= ABM.Category17,
	Category18Code	= ABM.Category18Code,
	Category18	= ABM.Category18,	
	Category19Code	= ABM.Category19Code,
	Category19	= ABM.Category19,
	Category20Code	= ABM.Category20Code,
	Category20	= ABM.Category20,
	Category21Code	= ABM.Category21Code,
	Category21	= ABM.Category21,
	Category22Code	= ABM.Category22Code,
	Category22	= ABM.Category22,
	Category23Code	= ABM.Category23Code,
	Category23	= ABM.Category23,
	Category24Code	= ABM.Category24Code,
	Category24	= ABM.Category24,
	Category25Code	= ABM.Category25Code,
	Category25	= ABM.Category25,	
	Category26Code	= ABM.Category26Code,
	Category26	= ABM.Category26,
	Category27Code	= ABM.Category27Code,
	Category27	= ABM.Category27,
	Category28Code	= ABM.Category28Code,
	Category28	= ABM.Category28,
	Category29Code	= ABM.Category29Code,
	Category29	= ABM.Category29,
	Category30Code	= ABM.Category30Code,
	Category30	= ABM.Category30

FROM 
	RD_ARMart.dbo.AR_Star_Customer SSC
	INNER JOIN RD_CommonWork.dbo.JDEABAddressBookMaster ABM
			ON (SSC.CustomerNumber 	= ABM.AddressNumber AND 
			    SSC.SourceID 	= ABM.SourceID)

----  UPDATE the BillToName from the JDEGSAddressBookWhosWho
UPDATE RD_ARMart.dbo.AR_Star_Customer
	SET BillToName = WW.MailingName
FROM 
	RD_ARMart.dbo.AR_Star_Customer SSC 
	INNER JOIN RD_CommonWork.dbo.JDEABAddressBookWhosWho WW
			ON (SSC.BillToNumber = WW.AddressNumber AND 
			    SSC.SourceID = WW.SourceID)
WHERE WW.WhosWholine = (SELECT MIN(WW1.WhosWholine) 
				FROM RD_CommonWork.dbo.JDEABAddressBookWhosWho WW1
				WHERE 	WW1.AddressNumber 	= WW.AddressNumber AND
						WW1.SourceID	= WW.SourceId)
						
 
----  UPDATE the Billing Address information FROM the JDEGSAddressBookByDate
UPDATE dbo.AR_Star_Customer
	SET BillToAddressLine01 	= ADB.AddressLine1,
		BillToAddressLine02 	= ADB.AddressLine2,
		BillToAddressLine03 	= ADB.AddressLine3,
		BillToAddressLine04 	= ADB.AddressLine4,
		BillToPostalCode 	= ADB.PostalCode,
		BillToCity 		= ADB.City,
		BillToCounty 		= ADB.County,
		BillToStateCode 	= ADB.StateCode,
		BillToState 		= ADB.State,
		BillToCountry 		= ADB.Country,
		BillToCountryCode 	= ADB.CountryCode
FROM RD_ARMart.dbo.AR_Star_Customer SSC 
	INNER JOIN RD_CommonWork.dbo.JDEABAddressBookAddressByDate ADB 
			ON (SSC.BillToNumber = ADB.AddressNumber AND  
				SSC.SourceID 	= ADB.SourceID)
WHERE  ADB.EffectiveFromDate = ''1''

-- Write out to log 
EXEC master.dbo.xp_logevent 65000,''Processing of BillTo Information Finished'',Informational


----------------------------------------------------------------------------------------------------------------------------
--   Will update the Parent  Number Information 		                         
----------------------------------------------------------------------------------------------------------------------------

-- Update the Parent Number FROM the JDEABAddressBookORGStructure
Update RD_ARMart.dbo.AR_Star_Customer
SET 
	ParentNumber = ABOS.ParentNumber
FROM
	RD_ARMart.dbo.AR_Star_Customer SSC 
	INNER JOIN RD_CommonWork.dbo.JDEABAddressBookORGStructure  ABOS ON
			( SSC.BillToNumber = ABOS.AddressNumber AND
			SSC.SourceID = ABOS.SourceID AND
			ABOS.StructureType = '' '')

----  UPDATE the Parent Name FROM the JDEGSAddressBookWhosWho
UPDATE RD_ARMart.dbo.AR_Star_Customer
SET 	ParentName = WW.MailingName
FROM 
	RD_ARMart.dbo.AR_Star_Customer SSC
	INNER JOIN RD_CommonWork.dbo.JDEABAddressBookWhosWho WW
			ON (SSC.ParentNumber = WW.AddressNumber AND 
			    SSC.SourceID = WW.SourceID)
WHERE WW.WhosWholine = (select MIN(WW1.WhosWholine) 
				FROM RD_CommonWork.dbo.JDEABAddressBookWhosWho WW1
				WHERE 	WW1.AddressNumber = WW.AddressNumber and 
			    		WW1.SourceID = WW.SourceID)

----  UPDATE the Parent Address information FROM the JDEGSAddressBookByDate
UPDATE RD_ARMart.dbo.AR_Star_Customer
SET 
	ParentAddressLine01 	= ADB.AddressLine1,
	ParentAddressLine02 	= ADB.AddressLine2,
	ParentAddressLine03 	= ADB.AddressLine3,
	ParentAddressLine04 	= ADB.AddressLine4,
	ParentPostalCode 	= ADB.PostalCode,
	ParentCity 		= ADB.City,
	ParentCounty 		= ADB.County,
	ParentStateCode 	= ADB.StateCode,
	ParentState 		= ADB.State,
	ParentCountry 		= ADB.Country,
	ParentCountryCode 	= ADB.CountryCode
FROM 
	RD_ARMart.dbo.AR_Star_Customer SSC
	INNER JOIN RD_CommonWork.dbo.JDEABAddressBookAddressByDate ADB 
			ON (SSC.ParentNumber = ADB.AddressNumber and 
				SSC.SourceID = ADB.SourceID)
WHERE 	    ADB.EffectiveFromDate = ''1'' 

-- Write out to log 
EXEC master.dbo.xp_logevent 65000,''Processing of Parent Information Finished'',Informational
-- Write out to log 
EXEC master.dbo.xp_logevent 65000,''End of AR Customer Refresh Procedure'',Informational
' 
END
GO
ALTER AUTHORIZATION ON [dbo].[USP_AR_Star_Customer_Refresh] TO  SCHEMA OWNER 
GO
/****** Object:  StoredProcedure [dbo].[USP_AR_Star_Ledger_Refresh]    Script Date: 5/30/13 1:01:31 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[USP_AR_Star_Ledger_Refresh]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE         PROCEDURE [dbo].[USP_AR_Star_Ledger_Refresh]
AS
----------------------------------------------------------------------------------------------------
--	Author:			c Copyright 2004 Andrews Consulting Group 
--	Procedure:		USP_AR_Star_Ledger_Refresh
--
--	Description:		User Stored Procedure To Update RD_ARMart.dbo.AR_Star_Facts
--				Deduction columns.  This User	
--				Stored Procedure Must Be Executed After Refresh Of 
-- 				AR_Facts - JDEOWARLedger. 
--
--	Application:		Accounts Receivable Data Mart
--
--
--Modification History
--
--Date			Modified By		Description
--01.01.2001 		<developer name>	<comments>
-----------------------------------------------------------------------------------------------------------

--DeductionID
UPDATE
	RD_ARMart.dbo.AR_Star_Facts
SET
	DeductionID = DM.DeductionID
FROM
	RD_ARMart.dbo.AR_Star_Facts Facts
	INNER JOIN RD_ARWork.dbo.JDEOWARARDeductionManagement DM ON
		Facts.SourceID		= DM.SourceID AND
		Facts.DocumentNumber	= DM.ClaimDocumentNumber AND
		Facts.DocumentType	= DM.ClaimDocumentType
WHERE
	InvoiceType=''I''

-- ''I'' DeductionActivityCode
UPDATE 
	RD_ARMart.dbo.AR_Star_Facts
SET
	DeductionActivityCode 	=	ISNULL(DA.ActivityReasonCode,''UA'')
FROM
	RD_ARMart.dbo.AR_Star_Facts Facts
	LEFT JOIN RD_ARWork.dbo.JDEOWARARDeductionActivity DA ON
		Facts.SourceID		=	DA.SourceID AND
		Facts.DeductionID	=	DA.DeductionID
WHERE
	InvoiceType=''I''
' 
END
GO
ALTER AUTHORIZATION ON [dbo].[USP_AR_Star_Ledger_Refresh] TO  SCHEMA OWNER 
GO
/****** Object:  StoredProcedure [dbo].[USP_AR_Type]    Script Date: 5/30/13 1:01:31 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[USP_AR_Type]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE              PROCEDURE [dbo].[USP_AR_Type]
AS
--------------------------------------------------------------------------------------------------
--	Author:			c Copyright 2004 Andrews Consulting Group
--	Procedure:		USP_AR_Type
--
--	Description:		Inserts Default Invoice Types Into AR_Star_Type
--				Dimension Table.  This User Stored Procedure Is Run 
--				After Mart Creation.
--
--	Application:		After Install,  AR_Star_Type
--
--
--Modification History
--
--Date			Modified By		Description
--01.01.2001 		<developer name>	<comments>
-------------------------------------------------------------------------------------------------

TRUNCATE Table RD_ARMart.dbo.AR_Star_Type

INSERT INTO 
	RD_ARMart.dbo.AR_Star_Type
VALUES
	('' '', 1, ''Rapid Decision 3.1'', ''I'', ''AR Ledger'', '' '', 0, GETDATE(), 0, ''1900/01/01'', 0, ''1900/01/01'', ''1900/01/01'')

INSERT INTO 
	RD_ARMart.dbo.AR_Star_Type
VALUES
	('' '', 1, ''Rapid Decision 3.1'', ''A'', ''AR Adjustments'', '' '', 0, GETDATE(), 0, ''1900/01/01'', 0, ''1900/01/01'', ''1900/01/01'')

INSERT INTO 
	RD_ARMart.dbo.AR_Star_Type
VALUES
	('' '', 1, ''Rapid Decision 3.1'', ''C'', ''AR Check Details'', '' '', 0, GETDATE(), 0, ''1900/01/01'', 0, ''1900/01/01'', ''1900/01/01'')
' 
END
GO
ALTER AUTHORIZATION ON [dbo].[USP_AR_Type] TO  SCHEMA OWNER 
GO
/****** Object:  StoredProcedure [dbo].[USP_Load_Orphans_AR]    Script Date: 5/30/13 1:01:31 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[USP_Load_Orphans_AR]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--	Author:			c Copyright 2004 Andrews Consulting Group 
--	Procedure:		USP_LOAD_ORPHANS_AR
--
--	Description:	User Stored Procedure To Populate the table
--					storing orphans.
--						
--
--	Application:	Load Orphans table
--
--
--Modification History
--Date				Modified By					Description
--08.18.2006		Andrews Consulting Group	Created.
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

CREATE  PROCEDURE [dbo].[USP_Load_Orphans_AR] @DateRun DATETIME
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;
	-- Clear the table of any previous records for this mart
	-- then load it again for a one time run.
	--DELETE FROM RD_COMMON.DBO.ORPHANS WHERE vchMart = ''ARMART''

	INSERT RD_COMMON.DBO.ORPHANS
	SELECT DISTINCT ''ARMART'', @DateRun, ''BusinessUnit'',T1.BusinessUnitCode, COUNT(T1.BusinessUnitCode)
	FROM RD_ARMART.DBO.AR_Star_Facts T1
	LEFT JOIN RD_Common.dbo.Star_BusinessUnit T2 ON
	T1.BusinessUnitCode = T2.BusinessUnitCode 
	AND T1.SOURCEID = T2.SOURCEID
	WHERE T2.BusinessUnitCode IS NULL
	GROUP BY T1.BusinessUnitCode
	
	INSERT RD_COMMON.DBO.ORPHANS
	SELECT DISTINCT ''ARMART'', @DateRun, ''Company'',T1.DocumentCompany, COUNT(T1.DocumentCompany)
    FROM RD_ARMART.DBO.AR_Star_Facts T1
    LEFT JOIN RD_Common.dbo.Star_Company T2 ON
	T2.CompanyNumber = T1.DocumentCompany
	AND T1.SOURCEID = T2.SOURCEID
	WHERE T2.CompanyNumber IS NULL
	GROUP BY T1.DocumentCompany

	INSERT RD_COMMON.DBO.ORPHANS
	SELECT DISTINCT ''ARMART'', @DateRun, ''Customer'', T1.BillToNumber, COUNT(T1.BillToNumber)
    FROM RD_ARMART.DBO.AR_Star_Facts T1
    LEFT JOIN RD_ARMART.DBO.AR_Star_Customer T2 ON
    T2.BillToNumber = T1.BillToNumber
	AND T1.SOURCEID = T2.SOURCEID
    WHERE T2.BillToNumber IS NULL
	GROUP BY T1.BillToNumber
	
	INSERT RD_COMMON.DBO.ORPHANS
	SELECT DISTINCT ''ARMART'', @DateRun, ''Deductions'', T1.DeductionActivityCode,COUNT(T1.DeductionActivityCode)
	FROM RD_ARMART.DBO.AR_Star_Facts T1
	LEFT JOIN RD_ARMART.DBO.AR_Star_Deductions T2 ON
	T1.DeductionActivityCode = T2.DeductionActivityCode
	AND T1.SOURCEID = T2.SOURCEID
	WHERE T2.DeductionActivityCode IS NULL
	GROUP BY T1.DeductionActivitycode

	INSERT RD_COMMON.DBO.ORPHANS
	SELECT DISTINCT ''ARMART'', @DateRun, ''DocumentType'', T1.DocumentType, COUNT(T1.DocumentType)
	FROM RD_ARMART.DBO.AR_Star_Facts T1
	LEFT JOIN RD_ARMART.DBO.AR_Star_Document T2 ON
	T2.DocumentTypeCode = T1.DocumentType 
	AND T1.SOURCEID = T2.SOURCEID
	WHERE T2.DocumentTypeCode IS NULL
	GROUP BY T1.Documenttype

	INSERT RD_COMMON.DBO.ORPHANS
	SELECT DISTINCT ''ARMART'', @DateRun, ''InvoiceType'', T1.InvoiceType, COUNT(T1.InvoiceType)	
	FROM RD_ARMART.DBO.AR_Star_Facts T1
	LEFT JOIN RD_ARMART.DBO.AR_Star_Type T2 ON
    T2.InvoiceType  = T1.InvoiceType
	WHERE T2.InvoiceType  IS NULL
	GROUP BY T1.InvoiceType

	INSERT RD_COMMON.DBO.ORPHANS
	SELECT DISTINCT ''ARMART'', @DateRun, ''GLDateID'', T1.GLDateID, COUNT(T1.GLDateID)	
	FROM RD_ARMART.DBO.AR_Star_Facts T1
	LEFT JOIN RD_ARMART.DBO.AR_Star_Dates T2 ON
    T2.CompanyDateID  = T1.GLDateID
	WHERE T2.CompanyDateID  IS NULL
	GROUP BY T1.GLDateID

	INSERT RD_COMMON.DBO.ORPHANS
	SELECT DISTINCT ''ARMART'', @DateRun, ''InvoiceDateID'', T1.InvoiceDateID, COUNT(T1.InvoiceDateID)	
	FROM RD_ARMART.DBO.AR_Star_Facts T1
	LEFT JOIN RD_ARMART.DBO.AR_Star_Dates T2 ON
    T2.CompanyDateID  = T1.InvoiceDateID
	WHERE T2.CompanyDateID  IS NULL
	GROUP BY T1.InvoiceDateID



END
' 
END
GO
ALTER AUTHORIZATION ON [dbo].[USP_Load_Orphans_AR] TO  SCHEMA OWNER 
GO
/****** Object:  Table [dbo].[AR_Star_Deductions]    Script Date: 5/30/13 1:01:31 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[AR_Star_Deductions]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[AR_Star_Deductions](
	[DeductionActivityCodeID] [char](10) NOT NULL,
	[Source] [char](20) NOT NULL,
	[SourceID] [int] NOT NULL,
	[DeductionActivityCode] [char](2) NOT NULL,
	[Description] [char](30) NOT NULL,
	[InsertIndicator] [int] NOT NULL,
	[InsertDate] [datetime] NOT NULL,
	[ChangeIndicator] [int] NOT NULL,
	[ChangeDate] [datetime] NOT NULL,
	[DeleteIndicator] [int] NOT NULL,
	[DeleteDate] [datetime] NOT NULL,
	[PurgeDate] [datetime] NOT NULL,
 CONSTRAINT [PK_AR_Star_Deductions] PRIMARY KEY CLUSTERED 
(
	[DeductionActivityCode] ASC,
	[SourceID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90, DATA_COMPRESSION = PAGE) ON [PRIMARY]
) ON [PRIMARY]
END
GO
SET ANSI_PADDING OFF
GO
ALTER AUTHORIZATION ON [dbo].[AR_Star_Deductions] TO  SCHEMA OWNER 
GO
/****** Object:  Table [dbo].[AR_Star_Facts]    Script Date: 5/30/13 1:01:31 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[AR_Star_Facts]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[AR_Star_Facts](
	[FactsExtID] [char](60) NOT NULL,
	[BusinessUnitID] [char](20) NOT NULL,
	[CustomerNumberID] [bigint] NOT NULL,
	[CompanyNumberID] [bigint] NOT NULL,
	[DeductionActivityCodeID] [char](10) NOT NULL,
	[DocumentID] [char](10) NOT NULL,
	[TypeID] [char](10) NOT NULL,
	[PaymentTermsID] [char](10) NOT NULL,
	[SourceID] [int] NOT NULL,
	[Source] [char](20) NOT NULL,
	[DocumentNumber] [int] NOT NULL,
	[DocumentType] [char](2) NOT NULL,
	[PayItem] [char](3) NOT NULL,
	[DocumentCompany] [char](5) NOT NULL,
	[InternalPaymentID] [int] NOT NULL,
	[FileLineIdentifier] [int] NOT NULL,
	[Company] [char](5) NOT NULL,
	[BusinessUnitCode] [char](12) NOT NULL,
	[BillToNumber] [int] NOT NULL,
	[InvoiceType] [char](1) NOT NULL,
	[DeductionID] [decimal](15, 0) NOT NULL,
	[GrossAmount] [decimal](20, 7) NOT NULL,
	[TransactionOpenAmount] [decimal](20, 7) NOT NULL,
	[AmountPaid] [decimal](20, 7) NOT NULL,
	[AmountWriteOff] [decimal](20, 7) NOT NULL,
	[TransactionOriginator] [char](10) NOT NULL,
	[OMSPONumber] [char](25) NOT NULL,
	[OMSInvoiceNumber] [char](30) NOT NULL,
	[OMSConfirmNumber] [int] NOT NULL,
	[CheckNumber] [char](25) NOT NULL,
	[TransactionWriteOffCode] [char](2) NOT NULL,
	[GLDateID] [bigint] NOT NULL,
	[InvoiceDateID] [bigint] NOT NULL,
	[OriginalDocumentNumber] [int] NOT NULL,
	[OriginalDocumentType] [char](2) NOT NULL,
	[OriginalOrderDocumentCompany] [char](5) NOT NULL,
	[OriginalPayItem] [char](3) NOT NULL,
	[TransactionDiscountAvailable] [decimal](20, 7) NOT NULL,
	[DiscountTaken] [decimal](20, 7) NOT NULL,
	[DueDateID] [bigint] NOT NULL,
	[DiscountDueDateID] [bigint] NOT NULL,
	[CurrencyCodeBase] [char](3) NOT NULL,
	[CurrencyConversionRateSpot] [decimal](20, 7) NOT NULL,
	[CurrencyCode] [char](3) NOT NULL,
	[GLPostedCode] [char](1) NOT NULL,
	[BatchNumber] [int] NOT NULL,
	[BatchType] [char](2) NOT NULL,
	[BatchDateID] [bigint] NOT NULL,
	[GLVoidDateID] [bigint] NOT NULL,
	[ForeignDiscountAvailable] [decimal](20, 7) NOT NULL,
	[ForeignDiscountTaken] [decimal](20, 7) NOT NULL,
	[TransactionDeductionCode] [char](2) NOT NULL,
	[CheckItemDateID] [bigint] NOT NULL,
	[PaymentAmount] [decimal](20, 7) NOT NULL,
	[DocumentNumberJE] [int] NOT NULL,
	[DocumentTypeJE] [char](2) NOT NULL,
	[AmountchargeBack] [decimal](20, 7) NOT NULL,
	[TransactionchargeBackCode] [char](2) NOT NULL,
	[TransactionRemark] [char](30) NOT NULL,
	[PayorAddressNumber] [int] NOT NULL,
	[PayStatus] [char](1) NOT NULL,
	[ForeignTaxAmount] [decimal](20, 7) NOT NULL,
	[ForeignTaxableAmount] [decimal](20, 7) NOT NULL,
	[ForeignNonTaxableAmount] [decimal](20, 7) NOT NULL,
	[ForeignOpenAmount] [decimal](20, 7) NOT NULL,
	[OriginalInvoiceNumber] [char](25) NOT NULL,
	[DateClosedID] [bigint] NOT NULL,
	[TransactionchargeBackAmount] [decimal](20, 7) NOT NULL,
	[TransactionPaymentAmount] [decimal](20, 7) NOT NULL,
	[TransactionDiscountAmount] [decimal](20, 7) NOT NULL,
	[TransactionWriteOffAmount] [decimal](20, 7) NOT NULL,
	[DeductionActivityCode] [char](2) NOT NULL,
	[DeductionManager] [int] NOT NULL,
	[CashReceived] [decimal](20, 7) NOT NULL,
	[PayItemExtensionNumber] [int] NOT NULL,
	[TaxableAmount] [decimal](15, 2) NOT NULL,
	[NonTaxableAmount] [decimal](15, 2) NOT NULL,
	[TaxAmount] [decimal](15, 2) NOT NULL,
	[TaxRateArea] [char](10) NOT NULL,
	[TaxExplanationCode] [char](2) NOT NULL,
	[GLOffset] [char](4) NOT NULL,
	[AccountID] [char](8) NOT NULL,
	[AlternatePayeeAddressNumber] [int] NOT NULL,
	[ARPostStatus] [char](1) NOT NULL,
	[OpenAmount] [decimal](20, 7) NOT NULL,
	[DiscountAvailable] [decimal](20, 7) NOT NULL,
	[GLBankAccount] [char](8) NOT NULL,
	[ObjectAccount] [char](6) NOT NULL,
	[Subsidiary] [char](8) NOT NULL,
	[SubledgerType] [char](1) NOT NULL,
	[SubLedger] [char](8) NOT NULL,
	[PaymentTerms] [char](3) NOT NULL,
	[WriteOffReasonCode] [char](2) NOT NULL,
	[InvoiceNumber] [char](25) NOT NULL,
	[PONumber] [char](8) NOT NULL,
	[PurchaseOrderDocumentType] [char](2) NOT NULL,
	[PurchaseOrderDocumentCompany] [char](5) NOT NULL,
	[OrderTypeCode] [char](2) NOT NULL,
	[LineNumber] [decimal](20, 7) NOT NULL,
	[SalesDocumentNumber] [int] NOT NULL,
	[SalesDocumentType] [char](2) NOT NULL,
	[SalesOrderDocumentCompany] [char](5) NOT NULL,
	[OrderSuffix] [char](3) NOT NULL,
	[Remark] [char](30) NOT NULL,
	[RecurFrequency] [char](2) NOT NULL,
	[PaymentInstrument] [char](1) NOT NULL,
	[VoidFlag] [char](1) NOT NULL,
	[DateInvoicePrintedID] [bigint] NOT NULL,
	[DateInvoiceClosedID] [bigint] NOT NULL,
	[AdjustmentReasonCode] [char](3) NOT NULL,
	[ParentNumber] [int] NOT NULL,
	[AmountCheckAmount] [decimal](20, 7) NOT NULL,
	[RemarkExplaination] [char](30) NOT NULL,
	[AmountDeduction] [decimal](20, 7) NOT NULL,
	[TransactionDeductionAmount] [decimal](20, 7) NOT NULL,
	[OrderType] [char](2) NOT NULL,
	[InsertIndicator] [int] NOT NULL,
	[InsertDate] [datetime] NOT NULL,
	[ChangeIndicator] [int] NOT NULL,
	[ChangeDate] [datetime] NOT NULL,
	[DeleteIndicator] [int] NOT NULL,
	[DeleteDate] [datetime] NOT NULL,
	[PurgeDate] [datetime] NOT NULL,
 CONSTRAINT [PK_AR_Star_Facts] PRIMARY KEY CLUSTERED 
(
	[DocumentNumber] ASC,
	[DocumentType] ASC,
	[PayItem] ASC,
	[DocumentCompany] ASC,
	[InternalPaymentID] ASC,
	[FileLineIdentifier] ASC,
	[InvoiceType] ASC,
	[PayItemExtensionNumber] ASC,
	[SourceID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90, DATA_COMPRESSION = PAGE) ON [PRIMARY]
) ON [PRIMARY]
END
GO
SET ANSI_PADDING OFF
GO
ALTER AUTHORIZATION ON [dbo].[AR_Star_Facts] TO  SCHEMA OWNER 
GO
/****** Object:  Table [dbo].[AR_Star_FactsExt]    Script Date: 5/30/13 1:01:31 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING OFF
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[AR_Star_FactsExt]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[AR_Star_FactsExt](
	[FactsExtID] [char](60) NOT NULL,
	[SourceID] [int] NOT NULL,
	[Source] [char](20) NOT NULL,
	[DocumentNumber] [int] NOT NULL,
	[DocumentType] [char](2) NOT NULL,
	[PayItem] [char](3) NOT NULL,
	[DocumentCompany] [char](5) NOT NULL,
	[InternalPaymentID] [int] NOT NULL,
	[FileLineIdentifier] [int] NOT NULL,
	[InvoiceType] [char](1) NOT NULL,
	[PayItemExtensionNumber] [int] NOT NULL,
	[GLPeriodNumber] [int] NOT NULL,
	[BalancedJE] [char](1) NOT NULL,
	[ModeForeignOrDomestic] [char](1) NOT NULL,
	[DomesticEntryWMultiCurrDistr] [char](1) NOT NULL,
	[CurrencyAmount] [decimal](20, 7) NOT NULL,
	[TaxExplanationCode] [char](2) NOT NULL,
	[ServiceDateTaxID] [bigint] NOT NULL,
	[AccountMode01] [char](1) NOT NULL,
	[GLCreditAccountID] [char](8) NOT NULL,
	[AccountMode02] [char](1) NOT NULL,
	[DateOfLastSentReminderID] [bigint] NOT NULL,
	[ReminderStopUntilDateID] [bigint] NOT NULL,
	[DateLastFeeCalculatedJulianID] [bigint] NOT NULL,
	[StatementDateID] [bigint] NOT NULL,
	[NumberOfRemindersToSend] [char](1) NOT NULL,
	[LevelOfLastSentReminder] [char](1) NOT NULL,
	[TotalNumberOfSentReminders] [int] NOT NULL,
	[CollectionReport] [char](1) NOT NULL,
	[CollectionReasonCode] [char](2) NOT NULL,
	[ApplyFinancecharges] [char](1) NOT NULL,
	[DelinquencyNotice] [char](1) NOT NULL,
	[ClearedValueDateID] [bigint] NOT NULL,
	[CommissionCode01] [int] NOT NULL,
	[Reference1] [char](25) NOT NULL,
	[UnitNumber] [char](8) NOT NULL,
	[AssetBusinessUnit] [char](12) NOT NULL,
	[AlphaName] [char](40) NOT NULL,
	[NumberOfPayments] [int] NOT NULL,
	[StatementNumber] [char](13) NOT NULL,
	[ClosedStatement] [char](1) NOT NULL,
	[ItemNumber] [int] NOT NULL,
	[Units] [decimal](20, 7) NOT NULL,
	[UnitOfMeasure] [char](2) NOT NULL,
	[CashBasisAccountingPostCode] [char](1) NOT NULL,
	[MiscellaneousCode01] [char](1) NOT NULL,
	[MiscellaneousCode02] [char](1) NOT NULL,
	[CashRcptsInvRegCode] [char](1) NOT NULL,
	[ARPosttoCashManagement] [char](1) NOT NULL,
	[UserReservedCode] [char](3) NOT NULL,
	[UserReservedDateID] [bigint] NOT NULL,
	[UserReservedAmount] [decimal](20, 7) NOT NULL,
	[UserReservedNumber] [int] NOT NULL,
	[UserReservedReference] [char](15) NOT NULL,
	[BatchRunIDInternal] [int] NOT NULL,
	[UserID] [char](10) NOT NULL,
	[ProgramID] [char](10) NOT NULL,
	[DateUpdatedID] [bigint] NOT NULL,
	[LastTimeUpdated] [int] NOT NULL,
	[DeductionReasonCode] [char](2) NOT NULL,
	[WorkStationID] [char](10) NOT NULL,
	[HistoricalExchangeRate] [decimal](20, 7) NOT NULL,
	[HistoricalDateID] [bigint] NOT NULL,
	[ShipmentNumber] [int] NOT NULL,
	[DeferredTaxStatusVAT] [char](1) NOT NULL,
	[OriginatingSystem] [char](1) NOT NULL,
	[CollectionManager] [char](10) NOT NULL,
	[CreditManager] [char](10) NOT NULL,
	[AmountToDistribute] [decimal](20, 7) NOT NULL,
	[AmountCurrencyToDistribute] [decimal](20, 7) NOT NULL,
	[NonRecoverableTaxAmount] [decimal](20, 7) NOT NULL,
	[ForeignNonRecoverableTax] [decimal](20, 7) NOT NULL,
	[PurgeFlag] [char](1) NOT NULL,
	[GenericFlag1] [char](1) NOT NULL,
	[GenericFlag2] [char](1) NOT NULL,
	[DocumentOrderNoInvoiceetc] [int] NOT NULL,
	[OrderCompanyOrderNumber] [char](5) NOT NULL,
	[SalesOrderTaxFlag] [char](1) NOT NULL,
	[NotificationPaymentDateID] [bigint] NOT NULL,
	[DateForExchangeRateJulianID] [bigint] NOT NULL,
	[OriginalDateNetDueID] [bigint] NOT NULL,
	[AmountOriginalGross] [decimal](20, 7) NOT NULL,
	[StatementPrintFlag] [char](1) NOT NULL,
	[ReceiptNumber] [char](25) NOT NULL,
	[CashReceiptLoggedCash] [char](1) NOT NULL,
	[CurrencyConversionYZ] [char](1) NOT NULL,
	[TriangulationLeg1] [decimal](15, 7) NOT NULL,
	[TriangulationLeg2] [decimal](15, 7) NOT NULL,
	[ForeignReceiptAmount] [decimal](20, 7) NOT NULL,
	[ReceiptTypeCode] [char](1) NOT NULL,
	[BankTapeReconcilliationRef] [char](8) NOT NULL,
	[DocumentCompanyJE] [char](5) NOT NULL,
	[VoidReasonCode] [char](3) NOT NULL,
	[ReceiptNSFVoidCode] [char](1) NOT NULL,
	[DocumentNumberVoidNSFJE] [int] NOT NULL,
	[DocumentTypeVoidNSFJE] [char](2) NOT NULL,
	[DocumentCompanyVoidNSFJE] [char](5) NOT NULL,
	[VoidNSFBatchType] [char](2) NOT NULL,
	[VoidNSFBatchNumber] [int] NOT NULL,
	[VoidNSFBatchDateID] [bigint] NOT NULL,
	[VoidNSFPostedCode] [char](1) NOT NULL,
	[CustomerBankAccountNumber] [char](20) NOT NULL,
	[BankTransitNumber] [char](20) NOT NULL,
	[CheckClearedDateID] [bigint] NOT NULL,
	[AutoCash] [char](1) NOT NULL,
	[AutoReceiptApplyAllCurrencies] [char](1) NOT NULL,
	[Uploadedfromtapebatchentry] [char](1) NOT NULL,
	[Clerk] [char](10) NOT NULL,
	[AutoReceiptsExecutionList] [char](10) NOT NULL,
	[AutoReceiptAlgorithmSequence] [int] NOT NULL,
	[APAutoReceiptProcessAlgorithm] [char](10) NOT NULL,
	[VersionHistory] [char](10) NOT NULL,
	[AutoReceiptStatusFlag] [char](1) NOT NULL,
	[DraftStatus] [char](1) NOT NULL,
	[Reference] [char](25) NOT NULL,
	[RenewalReferenceField] [decimal](15, 0) NOT NULL,
	[CashDiscDraftFlag] [char](1) NOT NULL,
	[DraftRegister] [char](13) NOT NULL,
	[PurgeCode] [char](1) NOT NULL,
	[CurrentUserID] [char](10) NOT NULL,
	[VoidFiscalYear] [int] NOT NULL,
	[VoidFiscalPeriod] [int] NOT NULL,
	[VoidFiscalCentury] [int] NOT NULL,
	[MulticurrIntercompanyPayment] [char](1) NOT NULL,
	[LocalizationReservedFlag] [char](1) NOT NULL,
	[PaymentItemDocumentType] [char](2) NOT NULL,
	[CompanyNumber] [char](5) NOT NULL,
	[RelatedPaymentID] [decimal](15, 0) NOT NULL,
	[RelatedPaymentLineID] [int] NOT NULL,
	[PaymentAmountForeign] [decimal](20, 7) NOT NULL,
	[ForeignChangedAmount] [decimal](20, 7) NOT NULL,
	[AmountForeignchargeback] [decimal](20, 7) NOT NULL,
	[AmountForeignDeduction] [decimal](20, 7) NOT NULL,
	[AmountGainLoss] [decimal](20, 7) NOT NULL,
	[DiscountAccountID] [char](8) NOT NULL,
	[WriteOffAccountID] [char](8) NOT NULL,
	[ChargebackReasonCode] [char](2) NOT NULL,
	[GLOffsetchargebacks] [char](4) NOT NULL,
	[ChargebackAccountID] [char](8) NOT NULL,
	[DeductionAccountID] [char](8) NOT NULL,
	[UserTypeInputCode] [char](2) NOT NULL,
	[UnappliedCashTransfer] [char](1) NOT NULL,
	[AssetItemNumber] [int] NOT NULL,
	[ClaimDocumentNumber] [int] NOT NULL,
	[ClaimDocumentType] [char](2) NOT NULL,
	[ClaimDocumentCompany] [char](5) NOT NULL,
	[ClaimDocumentPayItem] [char](3) NOT NULL,
	[PartialPayment] [char](1) NOT NULL,
	[DateInvoiceJournalDateJulianID] [bigint] NOT NULL,
	[RoundingAccountShortID] [char](8) NOT NULL,
	[RoundingAmount] [decimal](20, 7) NOT NULL,
	[DiscountReasonCode] [char](3) NOT NULL,
	[PayWhenPaidGroupNumber] [int] NOT NULL,
	[NettingTransactionID] [int] NOT NULL,
	[NettingDocumentNumber] [int] NOT NULL,
	[NettingLineID] [int] NOT NULL,
	[NettingStatusCode] [char](1) NOT NULL,
	[ActualReceiptDateID] [bigint] NOT NULL,
	[ActualDateInvoiceClosedID] [bigint] NOT NULL,
	[ReconciledCode] [char](1) NOT NULL,
	[Ref3AccountReconciliation] [char](8) NOT NULL,
	[GenericARReceiptMatchingRef1] [char](50) NOT NULL,
	[HNettingTransactionID] [int] NOT NULL,
	[HNettingDocumentNumber] [int] NOT NULL,
	[InsertIndicator] [int] NOT NULL,
	[InsertDate] [datetime] NOT NULL,
	[ChangeIndicator] [int] NOT NULL,
	[ChangeDate] [datetime] NOT NULL,
	[DeleteIndicator] [int] NOT NULL,
	[DeleteDate] [datetime] NOT NULL,
	[PurgeDate] [datetime] NOT NULL,
 CONSTRAINT [PK_AR_Star_FactsExt] PRIMARY KEY CLUSTERED 
(
	[DocumentNumber] ASC,
	[DocumentType] ASC,
	[PayItem] ASC,
	[DocumentCompany] ASC,
	[InternalPaymentID] ASC,
	[FileLineIdentifier] ASC,
	[InvoiceType] ASC,
	[PayItemExtensionNumber] ASC,
	[SourceID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90, DATA_COMPRESSION = PAGE) ON [PRIMARY]
) ON [PRIMARY]
END
GO
SET ANSI_PADDING OFF
GO
ALTER AUTHORIZATION ON [dbo].[AR_Star_FactsExt] TO  SCHEMA OWNER 
GO
/****** Object:  Table [dbo].[AR_Star_Type]    Script Date: 5/30/13 1:01:31 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING OFF
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[AR_Star_Type]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[AR_Star_Type](
	[TypeID] [char](1) NOT NULL,
	[SourceID] [int] NOT NULL,
	[Source] [char](20) NOT NULL,
	[InvoiceType] [char](1) NOT NULL,
	[InvoiceTypeDescription] [char](20) NOT NULL,
	[TypeClassification] [char](20) NOT NULL,
	[InsertIndicator] [int] NOT NULL,
	[InsertDate] [datetime] NOT NULL,
	[ChangeIndicator] [int] NOT NULL,
	[ChangeDate] [datetime] NOT NULL,
	[DeleteIndicator] [int] NOT NULL,
	[DeleteDate] [datetime] NOT NULL,
	[PurgeDate] [datetime] NOT NULL,
 CONSTRAINT [PK_AR_Star_Type] PRIMARY KEY CLUSTERED 
(
	[InvoiceType] ASC,
	[SourceID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90, DATA_COMPRESSION = PAGE) ON [PRIMARY]
) ON [PRIMARY]
END
GO
SET ANSI_PADDING OFF
GO
ALTER AUTHORIZATION ON [dbo].[AR_Star_Type] TO  SCHEMA OWNER 
GO
/****** Object:  View [dbo].[AR_Star_BusinessUnit]    Script Date: 5/30/13 1:01:31 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[dbo].[AR_Star_BusinessUnit]'))
EXEC dbo.sp_executesql @statement = N'
CREATE VIEW [dbo].[AR_Star_BusinessUnit]
AS
SELECT     *
FROM         RD_Common.dbo.Star_BusinessUnit

' 
GO
ALTER AUTHORIZATION ON [dbo].[AR_Star_BusinessUnit] TO  SCHEMA OWNER 
GO
/****** Object:  View [dbo].[AR_Star_BusinessUnitSecurity]    Script Date: 5/30/13 1:01:31 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[dbo].[AR_Star_BusinessUnitSecurity]'))
EXEC dbo.sp_executesql @statement = N'CREATE VIEW [dbo].[AR_Star_BusinessUnitSecurity]
AS
SELECT     *
FROM         RD_CommonWork.dbo.JDEBOBusinessUnitSecurity' 
GO
ALTER AUTHORIZATION ON [dbo].[AR_Star_BusinessUnitSecurity] TO  SCHEMA OWNER 
GO
/****** Object:  View [dbo].[AR_Star_Company]    Script Date: 5/30/13 1:01:31 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[dbo].[AR_Star_Company]'))
EXEC dbo.sp_executesql @statement = N'
CREATE VIEW [dbo].[AR_Star_Company]
AS
SELECT     *
FROM         RD_Common.dbo.Star_Company

' 
GO
ALTER AUTHORIZATION ON [dbo].[AR_Star_Company] TO  SCHEMA OWNER 
GO
/****** Object:  View [dbo].[AR_Star_Customer]    Script Date: 5/30/13 1:01:31 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[dbo].[AR_Star_Customer]'))
EXEC dbo.sp_executesql @statement = N'CREATE VIEW [dbo].[AR_Star_Customer] AS
SELECT     A.CustomerNumberID, A.SourceID, A.Source, A.CustomerNumber, CompanyNumber, CustomerName, 
					  BilltoNumber, BilltoName, BilltoAddressLine01, BilltoAddressLine02, BilltoAddressLine03, BilltoAddressLine04, 
                      BilltoPostalCode, BilltoCity, BilltoStateCode, BilltoState, BilltoCountryCode, BilltoCountry, BilltoCounty,   
                      ParentNumber, ParentName, ParentAddressLine01, ParentAddressLine02, ParentAddressLine03, 
                      ParentAddressLine04, ParentPostalCode, ParentCity, ParentCounty, ParentStateCode, ParentState, ParentCountry, ParentCountryCode, 
                      AlternatePayer, AverageDaysToPay, AmountInvoicedThisYear, LastPaidAmount, DateLastPaid, RelatedAddressNumber, CreditLimit, Category01Code, Category01, Category02Code, 
                      Category02, Category03Code, Category03, Category04Code, Category04, Category05Code, Category05, Category06Code, Category06, Category07Code,
                       Category07, Category08Code, Category08, Category09Code, Category09, Category10Code, Category10, Category11Code, Category11, 
                      Category12Code, Category12, Category13Code, Category13, Category14Code, Category14, Category15Code, Category15, Category16Code, Category16,
                       Category17Code, Category17, Category18Code, Category18, Category19Code, Category19, Category20Code, Category20, Category21Code, 
                      Category21, Category22Code, Category22, Category23Code, Category23, Category24Code, Category24, Category25Code, Category25, Category26Code,
                       Category26, Category27Code, Category27, Category28Code, Category28, Category29Code, Category29, Category30Code, Category30, InsertIndicator, 
                      InsertDate, ChangeIndicator, ChangeDate, DeleteIndicator, DeleteDate, PurgeDate, DateOfFirstInvoice, LastInvoiceDate, LastStatementDate,
						CASE WHEN B.Remark IS NULL
						THEN ''N/A'' ELSE B.Remark END AS Remark,
						CASE WHEN B.NameRemark IS NULL
						THEN ''N/A'' ELSE B.NameRemark END AS Remark2,
						CASE WHEN B.TextRemark IS NULL
						THEN ''N/A'' ELSE B.TextRemark END AS Remark3,
						CASE WHEN B.UDCValues IS NULL
						THEN ''N/A'' ELSE B.UDCValues END AS UDCValue
FROM         RD_Common.dbo.AR_Star_Customer A left outer join RD_CommonWork.dbo.Join_Customer_Suppl B
on B.CustomerNumber = A.BilltoNumber and B.Sourceid = A.Sourceid
' 
GO
ALTER AUTHORIZATION ON [dbo].[AR_Star_Customer] TO  SCHEMA OWNER 
GO
/****** Object:  View [dbo].[AR_Star_Customer_Orig]    Script Date: 5/30/13 1:01:31 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[dbo].[AR_Star_Customer_Orig]'))
EXEC dbo.sp_executesql @statement = N'CREATE VIEW [dbo].[AR_Star_Customer_Orig]
AS
SELECT     *
FROM         RD_Common.dbo.AR_Star_Customer' 
GO
ALTER AUTHORIZATION ON [dbo].[AR_Star_Customer_Orig] TO  SCHEMA OWNER 
GO
/****** Object:  View [dbo].[AR_Star_Dates]    Script Date: 5/30/13 1:01:31 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[dbo].[AR_Star_Dates]'))
EXEC dbo.sp_executesql @statement = N'CREATE VIEW [dbo].[AR_Star_Dates]
AS
SELECT     *
FROM         RD_Common.dbo.Star_Dates
' 
GO
ALTER AUTHORIZATION ON [dbo].[AR_Star_Dates] TO  SCHEMA OWNER 
GO
/****** Object:  View [dbo].[AR_Star_Document]    Script Date: 5/30/13 1:01:31 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[dbo].[AR_Star_Document]'))
EXEC dbo.sp_executesql @statement = N'
CREATE VIEW [dbo].[AR_Star_Document]
AS
SELECT     
	   Cast(substring(UserdefinedCodesValues,9,2) + Cast(SourceID as char(5)) as char(10)) as ''DocumentID'',
	   SourceID, 
	   cast (Source as Char(20)) as Source, 
	   Cast (Description02 as Char(30)) as ''Level1'', 
	   Cast (substring(UserdefinedCodesValues,9,2) as varchar(2)) as ''DocumentTypeCode'', 
	   Cast (Description01 as char(30)) as ''DocumentType'',
	   InsertIndicator,
	   InsertDate,
                ChangeIndicator,
                ChangeDate,
 	   DeleteIndicator,
	   DeleteDate,
	   PurgeDate
FROM     RD_CommonWork.dbo.JDEGSUserDefinedCodes
WHERE InstallSystemCode = ''00'' AND UserDefinedCode = ''DT''

' 
GO
ALTER AUTHORIZATION ON [dbo].[AR_Star_Document] TO  SCHEMA OWNER 
GO
/****** Object:  View [dbo].[AR_Star_PaymentTerms]    Script Date: 5/30/13 1:01:31 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[dbo].[AR_Star_PaymentTerms]'))
EXEC dbo.sp_executesql @statement = N'
CREATE view [dbo].[AR_Star_PaymentTerms]
as 
select *
from RD_Common.dbo.Star_PaymentTerms

' 
GO
ALTER AUTHORIZATION ON [dbo].[AR_Star_PaymentTerms] TO  SCHEMA OWNER 
GO
SET ANSI_PADDING ON

GO
/****** Object:  Index [IX_Deductions_DeductionActivityCodeID]    Script Date: 5/30/13 1:01:31 PM ******/
IF NOT EXISTS (SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID(N'[dbo].[AR_Star_Deductions]') AND name = N'IX_Deductions_DeductionActivityCodeID')
CREATE NONCLUSTERED INDEX [IX_Deductions_DeductionActivityCodeID] ON [dbo].[AR_Star_Deductions]
(
	[DeductionActivityCodeID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY]
GO
/****** Object:  Index [IX_Facts_BatchDate]    Script Date: 5/30/13 1:01:31 PM ******/
IF NOT EXISTS (SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID(N'[dbo].[AR_Star_Facts]') AND name = N'IX_Facts_BatchDate')
CREATE NONCLUSTERED INDEX [IX_Facts_BatchDate] ON [dbo].[AR_Star_Facts]
(
	[BatchDateID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY]
GO
/****** Object:  Index [IX_Facts_BillToNumber]    Script Date: 5/30/13 1:01:31 PM ******/
IF NOT EXISTS (SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID(N'[dbo].[AR_Star_Facts]') AND name = N'IX_Facts_BillToNumber')
CREATE NONCLUSTERED INDEX [IX_Facts_BillToNumber] ON [dbo].[AR_Star_Facts]
(
	[BillToNumber] ASC,
	[SourceID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY]
GO
SET ANSI_PADDING ON

GO
/****** Object:  Index [IX_Facts_BusinessUnitID]    Script Date: 5/30/13 1:01:31 PM ******/
IF NOT EXISTS (SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID(N'[dbo].[AR_Star_Facts]') AND name = N'IX_Facts_BusinessUnitID')
CREATE NONCLUSTERED INDEX [IX_Facts_BusinessUnitID] ON [dbo].[AR_Star_Facts]
(
	[BusinessUnitID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY]
GO
/****** Object:  Index [IX_Facts_CheckItemDate]    Script Date: 5/30/13 1:01:31 PM ******/
IF NOT EXISTS (SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID(N'[dbo].[AR_Star_Facts]') AND name = N'IX_Facts_CheckItemDate')
CREATE NONCLUSTERED INDEX [IX_Facts_CheckItemDate] ON [dbo].[AR_Star_Facts]
(
	[CheckItemDateID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY]
GO
/****** Object:  Index [IX_Facts_CompanyID]    Script Date: 5/30/13 1:01:31 PM ******/
IF NOT EXISTS (SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID(N'[dbo].[AR_Star_Facts]') AND name = N'IX_Facts_CompanyID')
CREATE NONCLUSTERED INDEX [IX_Facts_CompanyID] ON [dbo].[AR_Star_Facts]
(
	[CompanyNumberID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY]
GO
/****** Object:  Index [IX_Facts_CustomerNumberID]    Script Date: 5/30/13 1:01:31 PM ******/
IF NOT EXISTS (SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID(N'[dbo].[AR_Star_Facts]') AND name = N'IX_Facts_CustomerNumberID')
CREATE NONCLUSTERED INDEX [IX_Facts_CustomerNumberID] ON [dbo].[AR_Star_Facts]
(
	[CustomerNumberID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY]
GO
/****** Object:  Index [IX_Facts_DateClosed]    Script Date: 5/30/13 1:01:31 PM ******/
IF NOT EXISTS (SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID(N'[dbo].[AR_Star_Facts]') AND name = N'IX_Facts_DateClosed')
CREATE NONCLUSTERED INDEX [IX_Facts_DateClosed] ON [dbo].[AR_Star_Facts]
(
	[DateClosedID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY]
GO
/****** Object:  Index [IX_Facts_DateInvPrinted]    Script Date: 5/30/13 1:01:31 PM ******/
IF NOT EXISTS (SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID(N'[dbo].[AR_Star_Facts]') AND name = N'IX_Facts_DateInvPrinted')
CREATE NONCLUSTERED INDEX [IX_Facts_DateInvPrinted] ON [dbo].[AR_Star_Facts]
(
	[DateInvoicePrintedID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY]
GO
SET ANSI_PADDING ON

GO
/****** Object:  Index [IX_Facts_DeductionActivityCodeID]    Script Date: 5/30/13 1:01:31 PM ******/
IF NOT EXISTS (SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID(N'[dbo].[AR_Star_Facts]') AND name = N'IX_Facts_DeductionActivityCodeID')
CREATE NONCLUSTERED INDEX [IX_Facts_DeductionActivityCodeID] ON [dbo].[AR_Star_Facts]
(
	[DeductionActivityCodeID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY]
GO
/****** Object:  Index [IX_Facts_DiscountDueDate]    Script Date: 5/30/13 1:01:31 PM ******/
IF NOT EXISTS (SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID(N'[dbo].[AR_Star_Facts]') AND name = N'IX_Facts_DiscountDueDate')
CREATE NONCLUSTERED INDEX [IX_Facts_DiscountDueDate] ON [dbo].[AR_Star_Facts]
(
	[DiscountDueDateID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY]
GO
SET ANSI_PADDING ON

GO
/****** Object:  Index [IX_Facts_DocumentID]    Script Date: 5/30/13 1:01:31 PM ******/
IF NOT EXISTS (SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID(N'[dbo].[AR_Star_Facts]') AND name = N'IX_Facts_DocumentID')
CREATE NONCLUSTERED INDEX [IX_Facts_DocumentID] ON [dbo].[AR_Star_Facts]
(
	[DocumentID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY]
GO
/****** Object:  Index [IX_Facts_DueDate]    Script Date: 5/30/13 1:01:31 PM ******/
IF NOT EXISTS (SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID(N'[dbo].[AR_Star_Facts]') AND name = N'IX_Facts_DueDate')
CREATE NONCLUSTERED INDEX [IX_Facts_DueDate] ON [dbo].[AR_Star_Facts]
(
	[DueDateID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY]
GO
SET ANSI_PADDING ON

GO
/****** Object:  Index [IX_Facts_FactsExtID]    Script Date: 5/30/13 1:01:31 PM ******/
IF NOT EXISTS (SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID(N'[dbo].[AR_Star_Facts]') AND name = N'IX_Facts_FactsExtID')
CREATE NONCLUSTERED INDEX [IX_Facts_FactsExtID] ON [dbo].[AR_Star_Facts]
(
	[FactsExtID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY]
GO
/****** Object:  Index [IX_Facts_GLDate]    Script Date: 5/30/13 1:01:31 PM ******/
IF NOT EXISTS (SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID(N'[dbo].[AR_Star_Facts]') AND name = N'IX_Facts_GLDate')
CREATE NONCLUSTERED INDEX [IX_Facts_GLDate] ON [dbo].[AR_Star_Facts]
(
	[GLDateID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY]
GO
/****** Object:  Index [IX_Facts_GLVoidDate]    Script Date: 5/30/13 1:01:31 PM ******/
IF NOT EXISTS (SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID(N'[dbo].[AR_Star_Facts]') AND name = N'IX_Facts_GLVoidDate')
CREATE NONCLUSTERED INDEX [IX_Facts_GLVoidDate] ON [dbo].[AR_Star_Facts]
(
	[GLVoidDateID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY]
GO
/****** Object:  Index [IX_Facts_InternalPaymentID]    Script Date: 5/30/13 1:01:31 PM ******/
IF NOT EXISTS (SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID(N'[dbo].[AR_Star_Facts]') AND name = N'IX_Facts_InternalPaymentID')
CREATE NONCLUSTERED INDEX [IX_Facts_InternalPaymentID] ON [dbo].[AR_Star_Facts]
(
	[InternalPaymentID] ASC,
	[SourceID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY]
GO
/****** Object:  Index [IX_Facts_InvoiceDate]    Script Date: 5/30/13 1:01:31 PM ******/
IF NOT EXISTS (SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID(N'[dbo].[AR_Star_Facts]') AND name = N'IX_Facts_InvoiceDate')
CREATE NONCLUSTERED INDEX [IX_Facts_InvoiceDate] ON [dbo].[AR_Star_Facts]
(
	[InvoiceDateID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY]
GO
SET ANSI_PADDING ON

GO
/****** Object:  Index [IX_Facts_InvoiceType]    Script Date: 5/30/13 1:01:31 PM ******/
IF NOT EXISTS (SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID(N'[dbo].[AR_Star_Facts]') AND name = N'IX_Facts_InvoiceType')
CREATE NONCLUSTERED INDEX [IX_Facts_InvoiceType] ON [dbo].[AR_Star_Facts]
(
	[InvoiceType] ASC,
	[SourceID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY]
GO
SET ANSI_PADDING ON

GO
/****** Object:  Index [IX_Facts_PaymentTermsID]    Script Date: 5/30/13 1:01:31 PM ******/
IF NOT EXISTS (SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID(N'[dbo].[AR_Star_Facts]') AND name = N'IX_Facts_PaymentTermsID')
CREATE NONCLUSTERED INDEX [IX_Facts_PaymentTermsID] ON [dbo].[AR_Star_Facts]
(
	[PaymentTermsID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY]
GO
SET ANSI_PADDING ON

GO
/****** Object:  Index [IX_Facts_SalesDocTypeSource]    Script Date: 5/30/13 1:01:31 PM ******/
IF NOT EXISTS (SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID(N'[dbo].[AR_Star_Facts]') AND name = N'IX_Facts_SalesDocTypeSource')
CREATE NONCLUSTERED INDEX [IX_Facts_SalesDocTypeSource] ON [dbo].[AR_Star_Facts]
(
	[OMSConfirmNumber] ASC,
	[SalesDocumentType] ASC,
	[SourceID] ASC,
	[PayItem] ASC
)
INCLUDE ( 	[BatchDateID]) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY]
GO
SET ANSI_PADDING ON

GO
/****** Object:  Index [IX_Facts_SalesDocumentNumber]    Script Date: 5/30/13 1:01:31 PM ******/
IF NOT EXISTS (SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID(N'[dbo].[AR_Star_Facts]') AND name = N'IX_Facts_SalesDocumentNumber')
CREATE NONCLUSTERED INDEX [IX_Facts_SalesDocumentNumber] ON [dbo].[AR_Star_Facts]
(
	[SalesDocumentNumber] ASC,
	[CustomerNumberID] ASC,
	[PayStatus] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY]
GO
SET ANSI_PADDING ON

GO
/****** Object:  Index [IX_Facts_TypeID]    Script Date: 5/30/13 1:01:31 PM ******/
IF NOT EXISTS (SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID(N'[dbo].[AR_Star_Facts]') AND name = N'IX_Facts_TypeID')
CREATE NONCLUSTERED INDEX [IX_Facts_TypeID] ON [dbo].[AR_Star_Facts]
(
	[TypeID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY]
GO
SET ANSI_PADDING ON

GO
/****** Object:  Index [IX_OrderSuffix]    Script Date: 5/30/13 1:01:31 PM ******/
IF NOT EXISTS (SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID(N'[dbo].[AR_Star_Facts]') AND name = N'IX_OrderSuffix')
CREATE NONCLUSTERED INDEX [IX_OrderSuffix] ON [dbo].[AR_Star_Facts]
(
	[OrderSuffix] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY]
GO
/****** Object:  Index [IX_FactsExt_CheckClearedDate]    Script Date: 5/30/13 1:01:31 PM ******/
IF NOT EXISTS (SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID(N'[dbo].[AR_Star_FactsExt]') AND name = N'IX_FactsExt_CheckClearedDate')
CREATE NONCLUSTERED INDEX [IX_FactsExt_CheckClearedDate] ON [dbo].[AR_Star_FactsExt]
(
	[CheckClearedDateID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY]
GO
/****** Object:  Index [IX_FactsExt_ClearedValDateID]    Script Date: 5/30/13 1:01:31 PM ******/
IF NOT EXISTS (SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID(N'[dbo].[AR_Star_FactsExt]') AND name = N'IX_FactsExt_ClearedValDateID')
CREATE NONCLUSTERED INDEX [IX_FactsExt_ClearedValDateID] ON [dbo].[AR_Star_FactsExt]
(
	[ClearedValueDateID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY]
GO
/****** Object:  Index [IX_FactsExt_DateExchangeRate]    Script Date: 5/30/13 1:01:31 PM ******/
IF NOT EXISTS (SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID(N'[dbo].[AR_Star_FactsExt]') AND name = N'IX_FactsExt_DateExchangeRate')
CREATE NONCLUSTERED INDEX [IX_FactsExt_DateExchangeRate] ON [dbo].[AR_Star_FactsExt]
(
	[DateForExchangeRateJulianID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY]
GO
/****** Object:  Index [IX_FactsExt_DateInvJournal]    Script Date: 5/30/13 1:01:31 PM ******/
IF NOT EXISTS (SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID(N'[dbo].[AR_Star_FactsExt]') AND name = N'IX_FactsExt_DateInvJournal')
CREATE NONCLUSTERED INDEX [IX_FactsExt_DateInvJournal] ON [dbo].[AR_Star_FactsExt]
(
	[DateInvoiceJournalDateJulianID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY]
GO
/****** Object:  Index [IX_FactsExt_DateLastFeeCJ]    Script Date: 5/30/13 1:01:31 PM ******/
IF NOT EXISTS (SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID(N'[dbo].[AR_Star_FactsExt]') AND name = N'IX_FactsExt_DateLastFeeCJ')
CREATE NONCLUSTERED INDEX [IX_FactsExt_DateLastFeeCJ] ON [dbo].[AR_Star_FactsExt]
(
	[DateLastFeeCalculatedJulianID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY]
GO
/****** Object:  Index [IX_FactsExt_DateLastSR]    Script Date: 5/30/13 1:01:31 PM ******/
IF NOT EXISTS (SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID(N'[dbo].[AR_Star_FactsExt]') AND name = N'IX_FactsExt_DateLastSR')
CREATE NONCLUSTERED INDEX [IX_FactsExt_DateLastSR] ON [dbo].[AR_Star_FactsExt]
(
	[DateOfLastSentReminderID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY]
GO
/****** Object:  Index [IX_FactsExt_FactExtID]    Script Date: 5/30/13 1:01:31 PM ******/
IF NOT EXISTS (SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID(N'[dbo].[AR_Star_FactsExt]') AND name = N'IX_FactsExt_FactExtID')
CREATE NONCLUSTERED INDEX [IX_FactsExt_FactExtID] ON [dbo].[AR_Star_FactsExt]
(
	[FactsExtID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY]
GO
/****** Object:  Index [IX_FactsExt_HistoricalDate]    Script Date: 5/30/13 1:01:31 PM ******/
IF NOT EXISTS (SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID(N'[dbo].[AR_Star_FactsExt]') AND name = N'IX_FactsExt_HistoricalDate')
CREATE NONCLUSTERED INDEX [IX_FactsExt_HistoricalDate] ON [dbo].[AR_Star_FactsExt]
(
	[HistoricalDateID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY]
GO
/****** Object:  Index [IX_FactsExt_InternalPaymentID]    Script Date: 5/30/13 1:01:31 PM ******/
IF NOT EXISTS (SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID(N'[dbo].[AR_Star_FactsExt]') AND name = N'IX_FactsExt_InternalPaymentID')
CREATE NONCLUSTERED INDEX [IX_FactsExt_InternalPaymentID] ON [dbo].[AR_Star_FactsExt]
(
	[InternalPaymentID] ASC,
	[SourceID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY]
GO
/****** Object:  Index [IX_FactsExt_NotificationPayDt]    Script Date: 5/30/13 1:01:31 PM ******/
IF NOT EXISTS (SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID(N'[dbo].[AR_Star_FactsExt]') AND name = N'IX_FactsExt_NotificationPayDt')
CREATE NONCLUSTERED INDEX [IX_FactsExt_NotificationPayDt] ON [dbo].[AR_Star_FactsExt]
(
	[NotificationPaymentDateID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY]
GO
/****** Object:  Index [IX_FactsExt_OrigDateNetDue]    Script Date: 5/30/13 1:01:31 PM ******/
IF NOT EXISTS (SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID(N'[dbo].[AR_Star_FactsExt]') AND name = N'IX_FactsExt_OrigDateNetDue')
CREATE NONCLUSTERED INDEX [IX_FactsExt_OrigDateNetDue] ON [dbo].[AR_Star_FactsExt]
(
	[OriginalDateNetDueID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY]
GO
/****** Object:  Index [IX_FactsExt_ReminderStopUD]    Script Date: 5/30/13 1:01:31 PM ******/
IF NOT EXISTS (SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID(N'[dbo].[AR_Star_FactsExt]') AND name = N'IX_FactsExt_ReminderStopUD')
CREATE NONCLUSTERED INDEX [IX_FactsExt_ReminderStopUD] ON [dbo].[AR_Star_FactsExt]
(
	[ReminderStopUntilDateID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY]
GO
/****** Object:  Index [IX_FactsExt_StatementDateID]    Script Date: 5/30/13 1:01:31 PM ******/
IF NOT EXISTS (SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID(N'[dbo].[AR_Star_FactsExt]') AND name = N'IX_FactsExt_StatementDateID')
CREATE NONCLUSTERED INDEX [IX_FactsExt_StatementDateID] ON [dbo].[AR_Star_FactsExt]
(
	[StatementDateID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY]
GO
/****** Object:  Index [IX_FactsExt_UserResDateID]    Script Date: 5/30/13 1:01:31 PM ******/
IF NOT EXISTS (SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID(N'[dbo].[AR_Star_FactsExt]') AND name = N'IX_FactsExt_UserResDateID')
CREATE NONCLUSTERED INDEX [IX_FactsExt_UserResDateID] ON [dbo].[AR_Star_FactsExt]
(
	[UserReservedDateID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY]
GO
/****** Object:  Index [IX_Type_TypeID]    Script Date: 5/30/13 1:01:31 PM ******/
IF NOT EXISTS (SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID(N'[dbo].[AR_Star_Type]') AND name = N'IX_Type_TypeID')
CREATE NONCLUSTERED INDEX [IX_Type_TypeID] ON [dbo].[AR_Star_Type]
(
	[TypeID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY]
GO
